<?php
namespace app\backend\controller;

class RecycleOrderLong extends Backend {

	public function index() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$where = array();
		$condition = array();
		$condition['genre'] = $request->get('genre', '');
		if(0 < mb_strlen($condition['genre'], 'utf8')){
			$where['recycle_order_long.genre'] = $condition['genre'];
		};
		$condition['status'] = $request->get('status', '1');
		if((0 < mb_strlen($condition['status'], 'utf8')) && settype($condition['status'], 'integer')){
			$where['recycle_order_long.status'] = $condition['status'];
		};
		$condition['phone'] = $request->get('phone', '');
		if(0 < mb_strlen($condition['phone'], 'utf8')){
			$where['recycle_order_long.phone'] = array('like', '%%'.$condition['phone'].'%%');
		};
		$condition['number'] = $request->get('number', '');
		if(0 < mb_strlen($condition['number'], 'utf8')){
			$where['recycle_order_long.identifier'] = array('like', '%%'.$condition['number'].'%%');
		};
		$condition['address'] = $request->get('address', '');
		if(0 < mb_strlen($condition['address'], 'utf8')){
			$where['region.name_full'] = array('like', '%%'.$condition['address'].'%%');
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'recycle_order_long.*',
			'`region`.`name_full` AS `region_name`',
		);
		$order_select = \app\common\model\RecycleOrderLongModel::field($field)
			->join('region', '`region`.`id` = `recycle_order_long`.`region_district`', 'LEFT')
			->where($where)->order('recycle_order_long.id DESC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $order_select->render());
		
		$order_list = array();
		foreach($order_select as $RecycleCargoModel){
			$order = $RecycleCargoModel->getData();
			$order['frequency_name'] = $RecycleCargoModel->getFrequencyName();
			$order['status_name'] = $RecycleCargoModel->getStatusName();
			$order_list[] = $order;
		};
		$view->assign('data_list', $order_list);
		
		$status = \app\common\model\RecycleOrderLongModel::$status;
		$view->assign('status', $status);
		
		unset($where, $condition, $field, $order_select, $RecycleCargoModel);
		return $view->fetch();
		
	}
	
	public function cancel() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		$judge_2 = array_key_exists('cause', $parameter) && is_string($parameter['cause']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleOrderLongModel = \app\common\model\RecycleOrderLongModel::get($parameter['id']);
		if(null === $RecycleOrderLongModel){
			return $this->disposeJson(1, '目标未找到');
		}else if(0 === $RecycleOrderLongModel->status){
			return $this->disposeJson(1, '目标已被取消，请刷新后重试');
		};
		
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			$result = \app\common\model\RecycleOrderLongModel::where('id', $RecycleOrderLongModel->id)->where('status', 1)
				->update([
					'status' => 0,
					'cancel_time' => $time,
					'cancel_cause' => $parameter['cause'],
				]);
			if(false == $result){
				throw new \Exception('修改主订单状态失败');
			};
			
			\app\common\model\Model::commit();
			
		}catch(\Exception $error){
	
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $message);
			
		};
		
		return $this->disposeJson(0, '操作成功，即将刷新');
	}
	
	public function child() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$parent_id = $request->get('parent_id');
		if(1 !== preg_match('/^[0-9]{1,10}$/', $parent_id)){
			return $this->disposeHtml('参数校验失败', $view);
		};
		
		$where = array();
		$condition = array();
		$condition['genre'] = $request->get('genre', '');
		if(0 < mb_strlen($condition['genre'], 'utf8')){
			$where['recycle_order.genre'] = $condition['genre'];
		};
		$condition['status'] = $request->get('status', '');
		if((0 < mb_strlen($condition['status'], 'utf8')) && settype($condition['status'], 'integer')){
			$where['recycle_order.status'] = $condition['status'];
		};
		$condition['phone'] = $request->get('phone', '');
		if(0 < mb_strlen($condition['phone'], 'utf8')){
			$where['recycle_order.phone'] = array('like', '%%'.$condition['phone'].'%%');
		};
		$condition['number'] = $request->get('number', '');
		if(0 < mb_strlen($condition['number'], 'utf8')){
			$where['recycle_order.identifier'] = array('like', '%%'.$condition['number'].'%%');
		};
		$condition['staff_realname'] = $request->get('staff_realname', '');
		if(0 < mb_strlen($condition['staff_realname'], 'utf8')){
			$where['staff.realname'] = array('like', '%%'.$condition['staff_realname'].'%%');
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'recycle_order.*',
			'`staff`.`realname` AS `staff_realname`'
		);
		$order_select = \app\common\model\RecycleOrderModel::field($field)
			->join('staff', '`staff`.`id` = `recycle_order`.`staff_id`', 'LEFT')
			->where('recycle_order.parent_id', $parent_id)
			->where($where)->order('recycle_order.id DESC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $order_select->render());
		
		$order_list = array();
		foreach($order_select as $RecycleCargoModel){
			$order = $RecycleCargoModel->getData();
			$order['status_string'] = $RecycleCargoModel->getStatusName();
			$order['can_cancel'] = in_array($RecycleCargoModel->status, array(1, 2)) ? true : false;
			$order_list[] = $order;
		};
		$view->assign('data_list', $order_list);
		
		$genre = \app\common\model\RecycleOrderModel::$genre;
		$view->assign('genre', $genre);
		
		$status = \app\common\model\RecycleOrderModel::$status;
		$view->assign('status', $status);
		
		unset($where, $condition, $field, $order_select, $RecycleCargoModel);
		return $view->fetch();
		
	}
}